package com.herench.bunch.clinic.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.ADR_A19_QUERY_RESPONSE;
import ca.uhn.hl7v2.model.v24.message.ADR_A19;
import ca.uhn.hl7v2.model.v24.message.QRY_A19;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.clinic.handler.ProxyMsgCodeConstant;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class LoadMedicalInsuranceTypeConvert extends AbstractMessageConvert {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public Document doToMessage(RequestMessage requestMessage) throws Exception {
        Document inMessage = ProxyMsgTemplate.getInMsgTpl(ProxyMsgCodeConstant.LOAD_MEDICAL_INSURANCE_TYPE_CODE);
        Element requestElement = inMessage.getRootElement();
        QRY_A19 hl7 = (QRY_A19) requestMessage.getHl7Message();
        QRF qrf = hl7.getQRF();

        String MedicareID = qrf.getQrf1_WhereSubjectFilter(0).getValue();
        String HISPatientID = qrf.getQrf1_WhereSubjectFilter(1).getValue();

        requestElement.addElement("MedicareID").addText(MedicareID!=null?MedicareID:"");
        requestElement.addElement("HISPatientID").addText(HISPatientID!=null?HISPatientID:"");

        String res = inMessage.asXML();
        logger.info("200220 to HIS XML = "+ res);
        return inMessage;
    }

    @Override
    public Message doFromMessage(String message) throws Exception {
        Assert.notNull(message, "message 为空");
        logger.info("200220 from HIS XML = "+ message);
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message);
        Element res = doc.getRootElement();
        ADR_A19 adrA19 = new ADR_A19();
        adrA19.initQuickstart("ADR_A19", "ADR_A19", "P");
        MSH msh = adrA19.getMSH();
        initResponseMessageMSH(msh);

        MSA msa = adrA19.getMSA();
        msa.getErrorCondition().getCe1_Identifier().setValue(res.elementTextTrim("IsSuccessed"));
        msa.getErrorCondition().getCe2_Text().setValue(res.elementTextTrim("ErrorMsg"));

        QRD qrd = adrA19.getQRD();
        qrd.getQrd8_WhoSubjectFilter();
        Element request = doc.getRootElement();
        ADR_A19_QUERY_RESPONSE queryResponse = adrA19.getQUERY_RESPONSE(0);
        PID pid = queryResponse.getPID();


        pid.getPid1_SetIDPID().setValue(request.elementTextTrim("MedicareType"));

        return adrA19;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_A19 qbp = new QRY_A19();
        qbp.initQuickstart("QRY_A19", "QRY_A19", "A");
        MSH msh = qbp.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200220");
        msh.getMsh11_ProcessingID().getProcessingMode().setValue("QR");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue("B209");


        QRF qrf =qbp.getQRF();

        qrf.getQrf1_WhereSubjectFilter(0).setValue("6217250200034095643");
        qrf.getQrf1_WhereSubjectFilter(1).setValue("40021810311243270555");


        return qbp;
    }
}
